<template>
  <div>
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="(item,index) in hireType" :key="index" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="hfocus" />
        <div v-show="showTree" class="tree-box">
          <!-- 将原格式的label改为后端数据的name -->
          <el-tree :data="department" :props="{ label: 'name' }" @node-click="selectDepartment" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
      <el-form-item>
        <el-button @click="hCancel">取消</el-button>
        <el-button type="primary" @click="hSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addEmployee } from '@/api/employes'
import { tree2 } from '@/utils/tree'
// 导入聘用形式
import { getDepartments } from '@/api/departments'
import Type from '@/constant/employees'
const hireType = Type.hireType
export default {
  name: 'EmployAdd',
  data() {
    return {
      formData: {
        username: '十多个', // 用户名
        mobile: '13677777777', // 手机号
        formOfEmployment: '非正式', // 聘用形式
        workNumber: '435', // 工号
        departmentName: '人事部', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [{ required: true, message: '员工名字不能为空', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '转正时间不能为空', trigger: 'blur' }]
      },
      department: [],
      showTree: false // 部门信息树
    }
  },
  computed: {
    hireType() {
      // 不能直接使用导入的数据，必须要定义(写在computed中不需要用到data的响应式)
      return hireType
    }
  },
  methods: {
    // 取消
    hCancel() {
      this.$emit('close')
    },
    // 提交（确定）
    hSubmit() {
      // 手动校验
      // 表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // 调用添加员工
          this.doAdd()
        }
      })
    },
    // 添加员工
    async doAdd() {
      try {
        await addEmployee(this.formData)
        this.$emit('update-employee')
        this.$message.success('添加成功！')
      } catch (err) {
        console.log(err)
      }
    },
    // 获取部门信息
    async loadDepartments() {
      try {
        const { data } = await getDepartments()
        data.depts.shift()
        this.department = tree2(data.depts)
        // tree可见
        this.showTree = true
      } catch (err) {
        console.log(err)
      }
    },
    // 用户在tree中点击了某个节点
    selectDepartment(department) {
      // console.log(department, node)
      if (department.children.length === 0) {
        // 没有子节点可以选中,将当前部门赋值给input
        this.formData.departmentName = department.name
        // 选中后隐藏部门树
        this.showTree = false
      }
    },
    // 部门输入框获取焦点
    hfocus() {
      // 获取部门信息，
      this.loadDepartments()
    },
    // 定义重置表单的方法
    resetFrom() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
